package edu.nepu.flink.api.aggregation;

import edu.nepu.flink.api.bean.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Date 2024/2/28 20:57
 * @Created by chenshuaijun
 */
public class KeybyOperator {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataStreamSource<WaterSensor> elementSource = env.fromElements(
                new WaterSensor("s1", 1L, 1),
                new WaterSensor("s2", 2L, 11),
                new WaterSensor("s1", 3L, 3),
                new WaterSensor("s2", 5L, 5),
                new WaterSensor("s3", 4L, 5)
        );

        /**
         * 下面展示的是keyby这个算子的使用方式
         * 首先需要搞清楚两个概念之间的差别：分区和分组
         * 分区：这个是物理上的概念，他是在物理资源上将数据进行隔离，比如我们任务的并行度是2，那么就表示是有两个并行子任务，同时就是有两个分区
         * 分组：这个是逻辑上的概念，flink会将具有相同key的数据分到一个组中。同一个分区中可以包含多个组的数据
         */

        KeyedStream<WaterSensor, String> keyedStream = elementSource.keyBy(WaterSensor::getId);

        keyedStream.print();

        env.execute();

    }
}
